package puf.m2.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *  This class provides abilities to read/write data from/to a file stream.
 */
public class FileIO {
    public static String readLineFromFile(String filePath) {
        FileInputStream fis;
        BufferedReader buf;
        String result = "";
        try {
            fis = new FileInputStream(filePath);
            buf = new BufferedReader(new InputStreamReader(fis));
            result = buf.readLine();
            buf.close();
            fis.close();
        } catch (FileNotFoundException e) {
            System.out.println("[readLineFromFile] File " + filePath + " not found!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean writeToFile(String whatToWrite, String filePath) {
        FileOutputStream fos;
        BufferedWriter buf;
        boolean isSuccess = false;
        try {
            fos = new FileOutputStream(filePath);
            buf = new BufferedWriter(new OutputStreamWriter(fos));
            buf.write(whatToWrite);
            isSuccess = true;
            buf.close();
            fos.close();
        } catch (FileNotFoundException e) {
            System.out.println("[writeToFile] File " + filePath + " not found!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }
}
